# Load packages
library(ggplot2)
library(dplyr)
library(fixest)
# Set seed
set.seed(1996)
#############################
rm(list=setdiff(ls(), c('script', 'scripts', 'log_file')))
#############################
# Load Data
master <- readRDS('incumbent_challengerdf.rds')

# Create IV (WEB)
master$challenger_factor_web <- master$challenger_position_web_1
master$challenger_factor_web[master$candvotes == 1] <- 'No Challenger'
master$challenger_factor_web <- factor(master$challenger_factor_web, c( 'No Challenger', 'Moderate', 'Extreme'))

# Load in Paragraphs data
paragraphs <- readRDS('paragraphs_cleaned.rds')

colnames(paragraphs)

# Create Issue Binary Variables
master$abortion <- NA
master$education <- NA
master$energy <- NA
master$environment_climate <- NA
master$guns <- NA
master$healthcare <- NA
master$immigration <- NA

# Loop over all paragraphs
for(i in 1:nrow(paragraphs)){
  if(is.na(master$web_score[i])){
    next
  }
  # Subset to Candidate paragraph text
  temp <- subset(paragraphs, candidate == master$candidate[i] & 
                   state_postal == master$state_postal[i] &
                   cd == master$cd[i] &
                   year == master$year[i])
  
  # Create Binaries for if discussed or not
  master$abortion[i] <- ifelse(sum(temp$abortion) >= 1, 1, 0)
  master$education[i] <- ifelse(sum(temp$education) >= 1, 1, 0)
  master$energy[i] <- ifelse(sum(temp$energy) >= 1, 1, 0)
  master$environment_climate[i] <- ifelse(sum(temp$environment_climate) >= 1, 1, 0)
  master$guns[i] <- ifelse(sum(temp$guns) >= 1, 1, 0)
  master$healthcare[i] <- ifelse(sum(temp$healthcare) >= 1, 1, 0)
  master$immigration[i] <- ifelse(sum(temp$immigration) >= 1, 1, 0)
}

#############################
# Table I1: Issue Uptake Response
#############################
# abortion
m3_abortion <- glm(abortion ~ challenger_factor_web + as.factor(year) + as.factor(FECCandID), 
         data = subset(master, year != 2022), family = 'binomial')

# education
m3_education <- glm(education ~ challenger_factor_web + as.factor(year) + as.factor(FECCandID), 
         data = subset(master, year != 2022), family = 'binomial')

# energy
m3_energy <- glm(energy ~ challenger_factor_web + as.factor(year) + as.factor(FECCandID), 
         data = subset(master, year != 2022), family = 'binomial')

# environment_climate
m3_environment_climate <- glm(environment_climate ~ challenger_factor_web + as.factor(year) + as.factor(FECCandID), 
         data = subset(master, year != 2022), family = 'binomial')

# guns
m3_guns <- glm(guns ~ challenger_factor_web + as.factor(year) + as.factor(FECCandID), 
         data = subset(master, year != 2022), family = 'binomial')

# healthcare
m3_healthcare <- glm(healthcare ~ challenger_factor_web + as.factor(year) + as.factor(FECCandID), 
         data = subset(master, year != 2022), family = 'binomial')

# immigration
m3_immigration <- glm(immigration ~ challenger_factor_web + as.factor(year) + as.factor(FECCandID), 
         data = subset(master, year != 2022), family = 'binomial')

# Create Table 
stargazer(m3_abortion, m3_education, m3_energy, m3_environment_climate,
          keep = c('challenger_factor_web', 'Constant'))

stargazer(m3_guns, m3_healthcare, m3_immigration,
          keep = c('challenger_factor_web', 'Constant'))



